package com.didispace;

import javax.ws.rs.POST;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 翟永超
 * @create 2017/4/15.
 * @blog http://blog.didispace.com
 */
@RestController
public class DcController {

    @Autowired
    DiscoveryClient discoveryClient;

    // 实现/dc请求处理接口，通过DiscoveryClient对象，在日志中打印出服务实例的相关内容。
    // 方括号中的eureka-client就是通过Spring Cloud定义的DiscoveryClient接口在eureka的实现中获取到的所有服务清单。
    // 由于Spring Cloud在服务发现这一层做了非常好的抽象
    @GetMapping("/dc")
    public String dc() {
        String services = "Services: " + discoveryClient.getServices();
        System.out.println(services);
        return services;
    }
    
    @GetMapping("/get-dc")
    public String getDc(String id, String userName) {
        return id + userName;
    }
    
    @RequestMapping(value="/postDc", method = RequestMethod.POST)
    public String postDc(String id, String userName) {
        return id + userName;
    }
    
    @PostMapping(value="/postDc2")
    public String postDc2(@RequestBody User user, String id, String userName) {
//    	user.setId(id);
        return id + user.getUserName();
    }
    
    @GetMapping(value="/users/{id}")
    public User userId(@PathVariable("id") String id) {
    	User u = new User();
    	u.setId(id);
    	u.setUserName("xjf");
        return u;
    }
    
    @GetMapping(value="/feignTest")
    public User feignTest() {
    	try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	User u = new User();
    	u.setId("22");
    	u.setUserName("xjf");
        return u;
    }
}
